package com.crawler.harbour.task;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.crawler.harbour.model.Notice;
import com.crawler.harbour.service.INoticeService;
import com.crawler.harbour.utils.StorageDay;
import com.crawler.utils.RedisManager;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.http.HttpRequest;

/**
 * 通知定时器
 *
 * @author huang
 * @create 2018-05-31 21:47
 **/
@Component
public class NoticeTask extends StorageDay{

    @Autowired
    private INoticeService noticeService;

    //@Scheduled(cron = "0 07 16 * * ?")
    public void notice(){
    	if(!RedisManager.lock("ship:notice", 0)) {
    		return;
    	}
        EntityWrapper<Notice> ew=new EntityWrapper<>();
        ew.eq("status","0");
        List<Notice> noticeList = noticeService.selectList(ew);
        if(!noticeList.isEmpty()) {
        	try {
        		ObjectMapper objectMapper = new ObjectMapper();
        		objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
        		objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        		String writeValueAsString = objectMapper.writeValueAsString(noticeList);
				String result = HttpRequest.post("https://tms.hnscar.com/its/api/app/recSailDateChange").timeout(HttpRequest.TIMEOUT_DEFAULT).body(writeValueAsString).header("Content-Type", "application/json").execute().body();
	             System.out.println("+++++++++");
	             Map<String,Object> msg = JSONArray.parseObject(result);
	             if(msg!=null && Integer.valueOf(msg.get("statusCode").toString())==1){
	            	 for (Notice notice : noticeList){
	                     notice.setStatus(1);
	                     notice.setStorageDays(computeStorageDays(notice.getJkSailingDate(),notice.getCkSailingDate()));
	                     noticeService.updateById(notice);
	                 }
	             }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
/*    
    public static void main(String[] args) {
    	List<Notice> noticeList = Lists.newArrayList();
    	Notice notice = new Notice();
    	notice.setId(1);
    	noticeList.add(notice);
    	Notice notice1 = new Notice();
    	notice1.setId(2);
    	noticeList.add(notice1);
    	 String result = HttpUtil.post("https://tms.hnscar.com/its/api/app/recSailDateChange", JSON.toJSONString(noticeList));
    	 Map<String,Object> msg = JSONArray.parseObject(result);
    	 if(msg!=null && "1".equals(msg.get("statusCode"))){
    		 System.out.println();
    	 }
	}
*/
//    
//    public static void main(String[] args) {
//    	 String post = HttpUtil.post("https://tms.hnscar.com/its/api/app/recSailDateChange", JSON.toJSONString(Lists.newArrayList()));
//    	 System.out.println(post);
//	}
}